<?php

namespace app\admin\controller\user_profile\userArchive\psychologicalCounseling;

use app\admin\service\user_profile\userArchive\evaluationArchive\EvaluationArchiveService;
use app\admin\service\user_profile\userArchive\psychologicalCounseling\PsychologicalCounselingService;
use app\BaseController;
use app\common\Message;
use app\model\ConsultEvaluateModel;

class PsychologicalCounseling extends BaseController
{
    use Message;

    /**
     * 咨询预约概况
     * @return bool|string
     */
    public function getOverview(): bool|string
    {
        $param = $this->request->param();
        $param['unit_id'] = $this->userData['unit_id'];
        try {
            $PsychologicalCounseling = new PsychologicalCounselingService($this->app);
            if(!$PsychologicalCounseling->getOverview($param)) throw new \Exception($PsychologicalCounseling->getMessage());
            return success($PsychologicalCounseling->getResult(),$PsychologicalCounseling->getMessage());
        } catch (\Exception $e) {
            return error($e->getMessage(),$e->getCode(),[
                'line' => $e->getLine(),
                'file' => $e->getFile()
            ]);
        }
    }

    /**
     * 线上咨询记录
     * @return bool|string
     */
    public function getOnlineRecode(): bool|string
    {
        $param = $this->request->param();
        $param['unit_id'] = $this->userData['unit_id'];
        try {
            $PsychologicalCounseling = new PsychologicalCounselingService($this->app);
            if(!$PsychologicalCounseling->getOnlineRecode($param)) throw new \Exception($PsychologicalCounseling->getMessage());
            return success($PsychologicalCounseling->getResult(),$PsychologicalCounseling->getMessage());
        } catch (\Exception $e) {
            return error($e->getMessage(),$e->getCode(),[
                'line' => $e->getLine(),
                'file' => $e->getFile()
            ]);
        }
    }

    /**
     * 线下预约记录
     * @return bool|string
     */
    public function getOfflineRecode(): bool|string
    {
        $param = $this->request->param();
        $param['unit_id'] = $this->userData['unit_id'];
        try {
            $PsychologicalCounseling = new PsychologicalCounselingService($this->app);
            if(!$PsychologicalCounseling->getOfflineRecode($param)) throw new \Exception($PsychologicalCounseling->getMessage());
            return success($PsychologicalCounseling->getResult(),$PsychologicalCounseling->getMessage());
        } catch (\Exception $e) {
            return error($e->getMessage(),$e->getCode(),[
                'line' => $e->getLine(),
                'file' => $e->getFile()
            ]);
        }
    }

    /**
     * 评估记录
     * @return bool|string
     */
    public function getEvaluateRecode(): bool|string
    {
        try {
            $param = $this->request->param();
            if(check_array_key('isAbandon',$param)){
                //废弃档案
                $where = [
                    ['uid','=',$param['user_id']],
                    ['is_repeal','=',1],
                ];
            }else{
                //正常档案
                $where = [
                    ['uid','=',$param['user_id']],
                    ['is_repeal','=',0],
                ];
            }
            $return = ConsultEvaluateModel::query()->where($where)
                ->with(['consult','consultTime'])
                ->field('id,consult_id,question_id,is_end,end_time,situation,time_id')
                ->order('end_time desc')
                ->select();
            $ret = [];
            if(count($return)>0){
                $sort_by_month = new EvaluationArchiveService($this->app);
                $ret = $sort_by_month->sort_by_month($return->toArray(),'end_time',$param);
            }
            return success($ret);
        } catch (\Exception $e) {
            return error($e->getMessage(),$e->getCode(),[
                'line' => $e->getLine(),
                'file' => $e->getFile()
            ]);
        }
    }
}